<?php

namespace App\Console\Commands;

use App\Models\Book;
use App\Models\Recommend;
use App\Models\RecommendPosition;
use Illuminate\Console\Command;
use Faker\Generator as Faker;

class RandomRecommend extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'recommend:random';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'random recommend in database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(Faker $faker)
    {


        $this->warn('this command will clear you command table !');
        if (! $this->confirm('Do you wish to continue?')) {
            return;
        }


        $this->info('begin');
        Recommend::all()->each->delete();

        $rem_pos = RecommendPosition::all();

        $bar = $this->output->createProgressBar(count($rem_pos));

        foreach ($rem_pos as $p){

            $count = random_int(10,30);

            $bar->advance();

            for ($i = 0; $i < $count;$i++){

                $book = Book::normal()->inRandomOrder()->limit(1)->get()->first();
                $book->recommends()->create([
                    'recommend_positions_id' => $p->id,
                    'description' => $faker->paragraph,
                    'cover_url' => $faker->imageUrl()
                ]);
            }



        }

        $bar->finish();

        $this->info('');

        $this->info('success');

    }
}
